﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Ochkin.SquaredSoccer.Base;
using System.Collections.Generic;

namespace Ochkin.SquaredSoccer.UnitTest
{
    [TestClass]
    public sealed class GameTest
    {
        [TestMethod]
        public void TestGameError1()
        {
            Match game = new Match(5, 3, false, GameStyle.Random);
            var moves = GetMoves(game);
            moves.AddRange(new Tuple<Spot, Spot>[] {
                new Tuple<Spot, Spot>(new Spot(0, 0), new Spot(0, 1)),
                new Tuple<Spot, Spot>(new Spot(0, 1), new Spot(1, 0)),
                new Tuple<Spot, Spot>(new Spot(1, 0), new Spot(2, -1)),
                new Tuple<Spot, Spot>(new Spot(2, -1), new Spot(2, 0)),
                new Tuple<Spot, Spot>(new Spot(2, 0), new Spot(2, 1)),
                new Tuple<Spot, Spot>(new Spot(2, 1), new Spot(1, 2)),
                new Tuple<Spot, Spot>(new Spot(1, 2), new Spot(0, 2)),
                new Tuple<Spot, Spot>(new Spot(0, 2), new Spot(1, 3)),
                new Tuple<Spot, Spot>(new Spot(1, 3), new Spot(1, 2)),
                new Tuple<Spot, Spot>(new Spot(1, 2), new Spot(1, 1))
                //,
                //new Tuple<Spot, Spot>(new Spot(1, 1), new Spot(2, 2))
                });

            game.MakeHumanMove(new Direction[] { Direction.NewMix(SideOfEarth.North, SideOfEarth.East) });

            var result = game.GetComputerMove();
            Assert.IsNotNull(result.Value);
            foreach (var step in result.Value)
                Console.WriteLine(DirToStr(step));
        }

        private List<Tuple<Spot, Spot>> GetMoves(Match match)
        {
            const string MOVES = "moves";
            PrivateObject pObj = new PrivateObject(match);
            object result = pObj.GetField(MOVES);
            return (List<Tuple<Spot, Spot>>)result;
        }

        private static string DirToStr(Direction dir)
        {
            Dictionary<Direction, string> hashTable = new Dictionary<Direction, string>();
            hashTable.Add(Direction.NewSimple(SideOfEarth.North), "North");
            hashTable.Add(Direction.NewMix(SideOfEarth.North, SideOfEarth.East), "NorthEast");
            hashTable.Add(Direction.NewSimple(SideOfEarth.East), "East");
            hashTable.Add(Direction.NewMix(SideOfEarth.South, SideOfEarth.East), "SouthEast");
            hashTable.Add(Direction.NewSimple(SideOfEarth.South), "South");
            hashTable.Add(Direction.NewMix(SideOfEarth.South, SideOfEarth.West), "SouthWest");
            hashTable.Add(Direction.NewSimple(SideOfEarth.West), "West");
            hashTable.Add(Direction.NewMix(SideOfEarth.North, SideOfEarth.West), "NorthWest");
            return hashTable.ContainsKey(dir) ? hashTable[dir] : "unknown direction";
        }
    }
}
